package com.slmxz.tms.common.support;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slmxz.tms.common.util.StringExtendUtil;

import java.util.Map;

/**
 * @Author：albert
 * @Date：2024/9/27 22:37
 */
public class SqlKeyword {
    private static final String SQL_REGEX = "'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";
    private static final String EQUAL = "_equal";
    private static final String NOT_EQUAL = "_notequal";
    private static final String LIKE = "_like";
    private static final String NOT_LIKE = "_notlike";
    private static final String GT = "_gt";
    private static final String LT = "_lt";
    private static final String DATE_GT = "_dategt";
    private static final String DATE_EQUAL = "_dateequal";
    private static final String DATE_LT = "_datelt";
    private static final String IS_NULL = "_null";
    private static final String NOT_NULL = "_notnull";
    private static final String IGNORE = "_ignore";

    public SqlKeyword() {
    }

    public static void buildCondition(Map<String, Object> params, QueryWrapper<?> query) {
        if (CollectionUtil.isNotEmpty(params)) {
            params.forEach((k, v) -> {
                if (StrUtil.isNotBlank(k) && v != null && !k.endsWith("_ignore")) {
                    if (k.endsWith("_equal")) {
                        query.eq(getColumn(k, "_equal"), v);
                    } else if (k.endsWith("_notequal")) {
                        query.ne(getColumn(k, "_notequal"), v);
                    } else if (k.endsWith("_notlike")) {
                        query.notLike(getColumn(k, "_notlike"), v);
                    } else if (k.endsWith("_gt")) {
                        query.gt(getColumn(k, "_gt"), v);
                    } else if (k.endsWith("_lt")) {
                        query.lt(getColumn(k, "_lt"), v);
                    } else if (k.endsWith("_dategt")) {
                        query.gt(getColumn(k, "_dategt"), v);
                    } else if (k.endsWith("_dateequal")) {
                        query.eq(getColumn(k, "_dateequal"), v);
                    } else if (k.endsWith("_datelt")) {
                        query.lt(getColumn(k, "_datelt"), v);
                    } else if (k.endsWith("_null")) {
                        query.isNull(getColumn(k, "_null"));
                    } else if (k.endsWith("_notnull")) {
                        query.isNotNull(getColumn(k, "_notnull"));
                    } else {
                        query.like(getColumn(k, "_like"), v);
                    }

                }
            });
        }
    }

    private static String getColumn(String column, String keyword) {
        return StringExtendUtil.humpToUnderline(StringExtendUtil.removeSuffix(column, keyword));
    }

    public static String filter(String param) {
        return param == null ? null : param.replaceAll("(?i)'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql", "");
    }
}
